/** 
 * 2011-10-10 下午05:20:50 
 * NickServer.java 
 * author:sunb 
 */
package com.u6.udp.netty;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory;
import org.jboss.netty.channel.socket.DatagramChannelFactory;
import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.u6.udp.action.SendStreamAction;
import com.u6.udp.action.UdpLoginAction;
import com.u6.udp.util.MethodID;

/**
 * @author sunbin
 * 
 */
public class UdpServer {

	static Logger logger = LoggerFactory.getLogger(UdpServer.class);
	public void start() {
		try{
		UdpMessageActionClassMap actionClassMap = UdpMessageActionClassMap.getInstance();
		actionClassMap.registerRepeater(MethodID.Login.Request, UdpLoginAction.class);
		actionClassMap.registerRepeater(MethodID.ClientStream, SendStreamAction.class);

		ExecutorService workerExecutor = Executors.newCachedThreadPool();

		DatagramChannelFactory channelFactory = new NioDatagramChannelFactory(workerExecutor);

		ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(channelFactory);
		UdpChannelPipelineFactory channelPipelineFactory = new UdpChannelPipelineFactory();
		bootstrap.setPipelineFactory(channelPipelineFactory);
		// Enable broadcast
		bootstrap.setOption("broadcast", "false");

		bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(1024));

		bootstrap.bind(new InetSocketAddress("127.0.0.1",29001));
		}catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	public static void main(String[] args) {
		logger.debug("start udpserver");
		UdpServer server = new UdpServer();
		server.start();
		logger.debug("start success");
	}
}
